package com.example.asus.intentandbroacast;

import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class newDialerActivity extends AppCompatActivity {

    private  Button myButton=null;
    private  final String inf="Shazam";

    private Button registerButton = null;
    private Button unregisterButton = null;
    private SMSReceiverActivity smsReceiver = null;
    private static final String SMS_ACTION="android.provider.Telephony.SMS_RECEIVED";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_dialer);
        //显式Intent按钮
        Button dialer=(Button)findViewById(R.id.Button01);
        //隐式Intent输入框和按钮
        final EditText phoneNum=(EditText)findViewById(R.id.phoneNum);
        final Button button=(Button)findViewById(R.id.Button02);
        //IntentReceiver in XML
        myButton=(Button)findViewById(R.id.IntentReceiverButton);
        myButton.setOnClickListener(new MyButtonListener());
        //IntentReceiver in Java
        registerButton = (Button)findViewById(R.id.register);
        registerButton.setOnClickListener(new RegisterReceiverListener());
        unregisterButton = (Button)findViewById(R.id.unregister);
        unregisterButton.setOnClickListener(new UnRegisterReceiverListener());

        dialer.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent intent=new Intent(Intent.ACTION_DIAL, Uri.parse("tel://114"));
                startActivity(intent);
            }
        });

        button.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                String call = phoneNum.getText().toString();
                if (PhoneNumberUtils.isGlobalPhoneNumber(call)) {
                    Intent I = new Intent();
                    I.setAction(Intent.ACTION_CALL);
                    I.setData(Uri.parse("tel://"+call));
                    startActivity(I);
                }
                else{
                    Toast.makeText(newDialerActivity.this,R.string.notify_incorrect_phonenum,Toast.LENGTH_LONG).show();
                }
            }
        });
    }
    class MyButtonListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent(inf);
            intent.putExtra("testIntent","!!!!!!!!!!!!");
            sendBroadcast(intent);
        }
    }

    class RegisterReceiverListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            smsReceiver=new SMSReceiverActivity();
            IntentFilter filter=new IntentFilter();
            filter.addAction(SMS_ACTION);
            newDialerActivity.this.registerReceiver(smsReceiver,filter);
        }
    }

    class UnRegisterReceiverListener implements View.OnClickListener{

        @Override
        public void onClick(View v){
            newDialerActivity.this.unregisterReceiver(smsReceiver);
        }
    }
}